/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.max.oti.system.dao.hibernate;

import com.max.oti.system.dao.interfac.IHistoryPracticeDao;
import com.max.oti.system.dao.util.GenericEntityDao;
import com.max.oti.system.model.HistoryPractice;
import com.max.oti.system.model.Practice;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;

/**
 *
 * @author max
 */
public class HistoryPracticeDao extends GenericEntityDao<HistoryPractice, Long> implements IHistoryPracticeDao {

    @Override
    public List listCriterion(List<Criterion> listCriterion) {
        Criteria criteria = getSessionFactory().getCurrentSession()
                .createCriteria(HistoryPractice.class);
        for (int i = 0; i < listCriterion.size(); i++) {
            criteria.add(listCriterion.get(i));
        }
        List<HistoryPractice> listHP = criteria.list();
        for (int i = 0; i < listHP.size(); i++) {
            Practice practice = listHP.get(i).getIdPractice();
            for (int j = 0; j < practice.getAmplificationPracticeList().size(); j++) {
                Hibernate.initialize(practice.getAmplificationPracticeList().get(j));
            }
        }
        return criteria.list();
    }

    @Override
    public List listHQL(String hql, Class clazz) throws Exception {
        List list = getSessionFactory().getCurrentSession()
                .createQuery(hql)
                .setResultTransformer(Transformers.aliasToBean(clazz))
                .list();

//        for (int i = 0; i < list.size(); i++) {
//            Practice practice = ((HistoryPractice) list.get(i)).getIdPractice();
//            for (int j = 0; j < practice.getAmplificationPracticeList().size(); j++) {
//                Hibernate.initialize(practice.getAmplificationPracticeList().get(j));
//            }
//        }


        return list;
    }
}
